package com.woniuxy02;

import java.io.*;

/**
 * @author 作者
 * @create 2025-11-11 16:45
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        String source = "d:\\aaa\\a.pdf";
        String target = "d:\\aaa\\b.pdf";
        long begin = System.currentTimeMillis();
//        copyFile02(source, target);//685
//        copyFile03(source, target);//3077
        copyFile04(source, target);//336
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
    }

    //标准异常处理try-with-resource
    public static void copyFile06(String source, String target) {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));) {
            byte[] bytes = new byte[1024];
            int length = -1;
            while ((length = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //标准异常处理
    public static void copyFile05(String source, String target) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(source));
            bos = new BufferedOutputStream(new FileOutputStream(target));
            byte[] bytes = new byte[1024];
            int length = -1;
            while ((length = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //使用带缓冲区的字节流，一次读取一个字节数组
    public static void copyFile04(String source, String target) throws IOException {
        BufferedInputStream bis =
                new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos =
                new BufferedOutputStream(new FileOutputStream(target));
        byte[] bytes = new byte[1024];
        int length = -1;
        while ((length = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, length);
        }
        bos.close();
        bis.close();
    }

    //使用带缓冲区的字节流，一次读取一个字节
    public static void copyFile03(String source, String target) throws IOException {
        BufferedInputStream bis =
                new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos =
                new BufferedOutputStream(new FileOutputStream(target));
        int mybyte;
        while ((mybyte = bis.read()) != -1) {
            bos.write(mybyte);
        }
        bos.close();
        bis.close();
    }


    //使用字节流，一次读取一个字节数组
    public static void copyFile02(String source, String target) throws IOException {
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, length);
        }
        fos.close();
        fis.close();
    }

    //使用字节流，一次读取一个字节
    public static void copyFile01(String source, String target) throws IOException {
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        int mybyte;
        while ((mybyte = fis.read()) != -1) {
            fos.write(mybyte);
        }
        fos.close();
        fis.close();
    }
}
